package com.yy.db;

import com.yy.pojo.Goods;
import com.yy.pojo.ShoppingCart;
import com.yy.vo.Shopping;

import java.util.ArrayList;
import java.util.List;

public class GoodsDBUtil extends DBUtil {
    //查看所有的商品
    public List<Goods> getAllGoods() {
        List<Goods> list = new ArrayList<>();
        try {
            getConn();
            String sql = "select * from `goods`";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                Goods goods = new Goods(
                        rs.getInt(1),
                        rs.getString(2),
                        rs.getDouble("price"),
                        rs.getInt("amount"),
                        rs.getInt("sellamount"),
                        rs.getString("brand"),
                        rs.getString("country")
                );
                list.add(goods);
            }
        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            closeConn();
        }
        return list;
    }
    public List<Goods> getGoodsByConditon(String condition) {

        List<Goods> list =new ArrayList<>();
        try {
            getConn();
            String sql="";
            if(condition.equals("priceAsc")){
                sql="select * from goods order  by  "+"price asc";

            }else if(condition.equals("priceDesc")){
                sql="select * from goods order  by  "+"price desc";
            }else if(condition.equals("sellamountDesc")){
                sql="select * from goods order  by  "+"sellamount desc";
            }else if(condition.equals("sellamountAsc")){
                sql="select * from goods order  by  "+"sellamount Asc";
            }
            else{
                sql="select * from goods order  by  "+condition;
            }
            ps = conn.prepareStatement(sql);
            // ps.setObject(1,condition);
            rs = ps.executeQuery();
            while (rs.next()){
                Goods goods =new Goods(
                        rs.getInt("gid"),
                        rs.getString("title"),
                        rs.getDouble("price"),
                        rs.getInt("amount"),
                        rs.getInt("sellamount"),
                        rs.getString("brand"),
                        rs.getString("country"));
                list.add(goods);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeConn();
        }

        return list;
    }

    public Goods getGoodsDetails(String gid) {
        Goods goods=null;
        try{
            getConn();
            String sql="select * from `goods` where gid=?";
            ps=conn.prepareStatement(sql);
            ps.setObject(1, gid);
            rs=ps.executeQuery();
            while(rs.next()){
                goods =new Goods(
                        rs.getInt("gid"),
                        rs.getString("title"),
                        rs.getDouble("price"),
                        rs.getInt("amount"),
                        rs.getInt("sellamount"),
                        rs.getString("brand"),
                        rs.getString("country"));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            closeConn();
        }
        return goods;
    }

    public boolean addShoppingCart(String gid, String num) {
        int i=0;
        try{
            getConn();
            String sql="insert into `shoppingcart`(gid,count) values(?,?)";
            ps=conn.prepareStatement(sql);
            ps.setObject(1, gid);
            ps.setObject(2, num);
            i=ps.executeUpdate();
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            closeConn();
        }
        return i>0;
    }

    public List<Shopping> getShoppingCart() {
        List<Shopping> list=new ArrayList<>();
        try{
            getConn();
            String sql="select g.gid,g.title,s.count,g.price from goods g,shoppingcart s where g.gid=s.gid;";
            ps= conn.prepareStatement(sql);
            rs=ps.executeQuery();
            while(rs.next()){
                Shopping shopping=new Shopping(
                        rs.getInt(1),
                        rs.getString(2),
                        rs.getInt(3),
                        rs.getDouble(4),
                        null
                );
                list.add(shopping);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            closeConn();
        }
        return list;
    }
}
